package m202403.d29;

import java.util.Stack;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/3/29 19:50
 */
//232. 用栈实现队列
public class Q232 {
    public static class MyQueue {

        private Stack<Integer> a;
        private Stack<Integer> b;

        public MyQueue() {
            this.a = new Stack<>();
            this.b = new Stack<>();
        }

        public void push(int x) {
            a.push(x);
        }

        public int pop() {
            convert();
            return b.isEmpty() ? -1 : b.pop();
        }

        public int peek() {
            convert();
            return b.isEmpty() ? -1 : b.peek();
        }


        public boolean empty() {
            return a.isEmpty() && b.isEmpty();
        }

        private void convert() {
            if (b.isEmpty()) {
                while (!a.isEmpty()) {
                    b.push(a.pop());
                }
            }
        }
    }
}
